package rong;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.JPanel;

public class TreeListener implements MouseListener, MouseWheelListener, MouseMotionListener {

	TreePanel p;
	private TreeListener(TreePanel p) {
		this.p = p;
	}
	public static TreeListener listenPanel(TreePanel p) {
		TreeListener listener = new TreeListener(p);
		p.addMouseListener(listener);
		p.addMouseWheelListener(listener);
		p.addMouseMotionListener(listener);
		return listener;
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		Node node = p.getByXY(e.getX(), e.getY());
		if (node!=null) {
			if (MouseEvent.BUTTON1 == e.getButton()) 
				p.t.rotateLeft(node);
			else if (MouseEvent.BUTTON2 == e.getButton()) 
				p.t.delete((RBNode)node);
			else if (MouseEvent.BUTTON3 == e.getButton()) 
				p.t.rotateRight(node);
			p.repaint();
		}
	}
	@Override
	public void mousePressed(MouseEvent e) {
	}
	@Override
	public void mouseReleased(MouseEvent e) {
	}
	@Override
	public void mouseEntered(MouseEvent e) {
	}
	@Override
	public void mouseExited(MouseEvent e) {
	}
	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
		Node node = p.getByXY(e.getX(), e.getY());
		if (node!=null) {
			p.t.reverseColor((RBNode)node);
			p.repaint();
		}
	}
	@Override
	public void mouseDragged(MouseEvent e) {
		 System.out.println("dragged x坐标:"+e.getX()+"  y坐标:"+e.getY()) ;  
	}
	@Override
	public void mouseMoved(MouseEvent e) {
	}

}
